package GUI;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.JComboBox;
import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import MessagePacks.OneProcessPack;
import javax.swing.JTextField;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class MainScreenStudent extends MasterGUI{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnNewRequest;
	private JButton btnTrace;
	private JButton btnMessages;
	private MainGUI mymaingui;
	private JPanel panel;
	private NewRequestScreen nrscreen;
	private JPanel panel_1;
	private JPanel panel_2;
	private JButton btnSlcPrc;
	private JComboBox<String> comboBox;
	private GetAllPack gtPrcPack, time;
	private OneProcessPack prcPack;
	private JTextField textPhaseField;
	private JTextField textTimeField;
	private JButton btnOk;
	
	
	public MainScreenStudent(MainGUI maingui) {
		
		super(maingui);
		this.mymaingui = maingui;
		getContentPane().setLayout(null);

		getAllProccess();
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(0, 40, 176, 114);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		setBtnNewRequest();
		setBtnTrace();
		setBtnMessages();
	
		JLabel lblWelcomeToIcm = new JLabel("Welcome to ICM");
		lblWelcomeToIcm.setBounds(0, 0, 884, 42);
		lblWelcomeToIcm.setFont(new Font("Dialog", Font.BOLD, 22));
		lblWelcomeToIcm.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeToIcm.setAlignmentX(Component.CENTER_ALIGNMENT);
		getContentPane().add(lblWelcomeToIcm);
		
		panel_1 = new JPanel();
		panel_1.setBounds(212, 40, 661, 512);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		panel_1.setVisible(false);
			
		setBtnSlcPrc();
		setComboBox();
			
		JLabel lblProcessId = new JLabel("Process ID");
		lblProcessId.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblProcessId.setHorizontalAlignment(SwingConstants.CENTER);
		lblProcessId.setBounds(57, 18, 116, 23);
		panel_1.add(lblProcessId);
		
		panel_2 = new JPanel();
		panel_2.setBounds(10, 103, 408, 379);
		panel_1.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblPahse = new JLabel("Process Phase : ");
		lblPahse.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPahse.setBounds(28, 55, 117, 22);
		panel_2.add(lblPahse);
		
		
		JLabel lblTimeToFinish = new JLabel("Time to finish phase : ");
		lblTimeToFinish.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTimeToFinish.setBounds(28, 106, 159, 22);
		panel_2.add(lblTimeToFinish);
		
		
		textPhaseField = new JTextField();
		textPhaseField.setBounds(181, 58, 86, 20);
		panel_2.add(textPhaseField);
		textPhaseField.setColumns(10);
		
		
		textTimeField = new JTextField();
		textTimeField.setBounds(181, 109, 86, 20);
		panel_2.add(textTimeField);
		textTimeField.setColumns(10);
		
		btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_1.setVisible(false);
				panel_2.setVisible(false);
			}
		});
		btnOk.setBounds(139, 167, 89, 23);
		panel_2.add(btnOk);
		
		panel_2.setVisible(false);
		
			
		
		
	}



	public JButton getBtnNewRequest() {
		return btnNewRequest;
	}



	public void setBtnNewRequest() {
		btnNewRequest = new JButton("New Request");
		btnNewRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"new request button pressed ");
				nrscreen = new NewRequestScreen(getMaingui().usr.GetUser(),getMaingui());
				nrscreen.setVisible(true);
				close();
				
			}
		});
		btnNewRequest.setBounds(10, 11, 156, 23);
		panel.add(btnNewRequest);
	}



	public JButton getBtnTrace() {
		return btnTrace;
	}



	public void setBtnTrace() {
		btnTrace = new JButton("Trace Request");
		btnTrace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_1.setVisible(true);	
			}
		});
		btnTrace.setBounds(10, 45, 156, 23);
		panel.add(btnTrace);
	}



	public JButton getBtnMessages() {
		return btnMessages;
	}



	public void setBtnMessages() {
		btnMessages = new JButton("Messages");
		btnMessages.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Messages button pressed ");
				WatchMessages watch = new WatchMessages(mymaingui);
				watch.setVisible(true);
				close();
			}
		});
		btnMessages.setBounds(10, 79, 156, 23);
		panel.add(btnMessages);
	}

	public JButton getBtnSlcPrc() {
		return btnSlcPrc;
	}

	public void setBtnSlcPrc() {
		btnSlcPrc = new JButton("Select");
		btnSlcPrc.setEnabled(false);
		btnSlcPrc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getProccessSelect();
				GetTime();
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Process selected ");
				textTimeField.setEditable(true);
				textPhaseField.setEditable(true);
				panel_2.setVisible(true);
				textPhaseField.setText(String.valueOf(prcPack.getCurrentPhase()));
				textTimeField.setText(String.valueOf(time.getTime()));
				textTimeField.setEditable(false);
				textPhaseField.setEditable(false);
			}
		});
		btnSlcPrc.setBounds(259, 51, 89, 23);
		panel_1.add(btnSlcPrc);
	}

	public JComboBox<String> getComboBox() {
		return comboBox;
	}

	public void setComboBox() {
		comboBox = new JComboBox<String>();
		comboBox.addItem("");
		fillBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				btnSlcPrc.setEnabled(true);
			}
		});
		comboBox.setBounds(57, 52, 116, 20);
		panel_1.add(comboBox);
	}
	
	
	private void getAllProccess(){
		gtPrcPack = new GetAllPack(MessagePack.traceProcess);
		gtPrcPack.setInitID(mymaingui.usr.GetUser());
		mymaingui.client.handleMessageFromClientUI(gtPrcPack);
		gtPrcPack= (GetAllPack) mymaingui.client.getMessage();
		
	}
	
	private void getProccessSelect(){
		prcPack = new OneProcessPack(Integer.parseInt((String) comboBox.getSelectedItem()));
		mymaingui.client.handleMessageFromClientUI(prcPack);
		prcPack= (OneProcessPack) mymaingui.client.getMessage();
		
	}
	
	private void fillBox(){
		Iterator<String> iter = gtPrcPack.nArray.iterator();
		while (iter.hasNext()) {
			String procId = iter.next();
			comboBox.addItem(procId);
		}
	}
	private void GetTime(){
		time= new GetAllPack(MessagePack.getTime);
		time.setInitID(prcPack.getProcessID());
		mymaingui.client.handleMessageFromClientUI(time);
		time= (GetAllPack) mymaingui.client.getMessage();		
		
	}
}
